package com.baiqian.doupo.tileentityrenderer;

import com.baiqian.doupo.effect.EffectRegistry;
import com.baiqian.doupo.item.DanYao;
import com.baiqian.doupo.item.ItemRegistry;
import com.baiqian.doupo.tileentity.HuoDingTileEntity;
import com.mojang.blaze3d.matrix.MatrixStack;
import net.minecraft.block.HorizontalBlock;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.IRenderTypeBuffer;
import net.minecraft.client.renderer.ItemRenderer;
import net.minecraft.client.renderer.model.IBakedModel;
import net.minecraft.client.renderer.model.ItemCameraTransforms;
import net.minecraft.client.renderer.tileentity.TileEntityRenderer;
import net.minecraft.client.renderer.tileentity.TileEntityRendererDispatcher;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.particles.ParticleTypes;
import net.minecraft.potion.EffectInstance;
import net.minecraft.state.DirectionProperty;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.SoundEvents;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.vector.Vector3f;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;

import java.util.Collection;
import java.util.List;
import java.util.Random;
import java.util.concurrent.atomic.AtomicBoolean;

public class HuoDingTER extends TileEntityRenderer<HuoDingTileEntity> {

    public HuoDingTER(TileEntityRendererDispatcher rendererDispatcherIn) {
        super(rendererDispatcherIn);
    }

    public int ticks;
    public float field_195523_f;
    public float field_195524_g;
    public float field_195525_h;
    public float field_195526_i;
    public float nextPageTurningSpeed;
    public float pageTurningSpeed;
    public float nextPageAngle;
    public float pageAngle;
    public float field_195531_n;
    private static final Random random = new Random();
    private boolean is_render=false;

    private final static ItemStack[] qian_kou_1 = {
            new ItemStack(ItemRegistry.qian_kou_1_1.get()),
            new ItemStack(ItemRegistry.qian_kou_1_2.get()),
            new ItemStack(ItemRegistry.qian_kou_1_3.get()),
            new ItemStack(ItemRegistry.qian_kou_1_4.get()),
            new ItemStack(ItemRegistry.qian_kou_1_5.get()),
            new ItemStack(ItemRegistry.qian_kou_1_6.get()),
            new ItemStack(ItemRegistry.qian_kou_1_7.get()),
            new ItemStack(ItemRegistry.qian_kou_1_8.get()),
    };
    private final static ItemStack[] qian_kou_fire = {
            new ItemStack(ItemRegistry.qian_kou_fire_1.get()),
            new ItemStack(ItemRegistry.qian_kou_fire_2.get()),
            new ItemStack(ItemRegistry.qian_kou_fire_3.get()),
            new ItemStack(ItemRegistry.qian_kou_fire_4.get()),
            new ItemStack(ItemRegistry.qian_kou_fire_5.get()),
            new ItemStack(ItemRegistry.qian_kou_fire_6.get()),
            new ItemStack(ItemRegistry.qian_kou_fire_7.get()),
            new ItemStack(ItemRegistry.qian_kou_fire_8.get()),
    };
    private final static ItemStack[] cai_liao_bg = {
            new ItemStack(ItemRegistry.cai_liao_bg_0.get()),
            new ItemStack(ItemRegistry.cai_liao_bg_1.get()),
            new ItemStack(ItemRegistry.cai_liao_bg_2.get()),
            new ItemStack(ItemRegistry.cai_liao_bg_3.get()),
            new ItemStack(ItemRegistry.cai_liao_bg_4.get()),
            new ItemStack(ItemRegistry.cai_liao_bg_5.get()),
            new ItemStack(ItemRegistry.cai_liao_bg_6.get()),
            new ItemStack(ItemRegistry.cai_liao_bg_7.get()),
            new ItemStack(ItemRegistry.cai_liao_bg_8.get()),
    };
    private final static ItemStack[] cai_liao_board_time = {
            new ItemStack(ItemRegistry.cai_liao_board_time_0.get()),
            new ItemStack(ItemRegistry.cai_liao_board_time_1.get()),
            new ItemStack(ItemRegistry.cai_liao_board_time_2.get()),
            new ItemStack(ItemRegistry.cai_liao_board_time_3.get()),
            new ItemStack(ItemRegistry.cai_liao_board_time_4.get()),
            new ItemStack(ItemRegistry.cai_liao_board_time_5.get()),
            new ItemStack(ItemRegistry.cai_liao_board_time_6.get()),
            new ItemStack(ItemRegistry.cai_liao_board_time_7.get()),
            new ItemStack(ItemRegistry.cai_liao_board_time_8.get()),
            new ItemStack(ItemRegistry.cai_liao_board_time_9.get()),
            new ItemStack(ItemRegistry.cai_liao_board_time_10.get()),
            new ItemStack(ItemRegistry.cai_liao_board_time_11.get()),
            new ItemStack(ItemRegistry.cai_liao_board_time_12.get()),
    };
    private final static ItemStack[] cai_liao_board_point = {
            new ItemStack(ItemRegistry.cai_liao_board_point_0.get()),
            new ItemStack(ItemRegistry.cai_liao_board_point_1.get()),
            new ItemStack(ItemRegistry.cai_liao_board_point_2.get()),
            new ItemStack(ItemRegistry.cai_liao_board_point_3.get()),
            new ItemStack(ItemRegistry.cai_liao_board_point_4.get()),
            new ItemStack(ItemRegistry.cai_liao_board_point_5.get()),
            new ItemStack(ItemRegistry.cai_liao_board_point_6.get()),
            new ItemStack(ItemRegistry.cai_liao_board_point_7.get()),
            new ItemStack(ItemRegistry.cai_liao_board_point_8.get()),
            new ItemStack(ItemRegistry.cai_liao_board_point_9.get()),
            new ItemStack(ItemRegistry.cai_liao_board_point_10.get()),
            new ItemStack(ItemRegistry.cai_liao_board_point_11.get()),
            new ItemStack(ItemRegistry.cai_liao_board_point_12.get()),
    };

    @OnlyIn(Dist.CLIENT)
    @Override
    public void render(HuoDingTileEntity tileEntityIn, float partialTicks, MatrixStack matrixStackIn, IRenderTypeBuffer bufferIn, int combinedLightIn, int combinedOverlayIn) {
        List<ItemStack> list = tileEntityIn.getMakingItem();

        ++this.ticks;
        if(list.size()>0||tileEntityIn.getFinishItem().size()>0) {
            this.pageTurningSpeed = this.nextPageTurningSpeed;
            this.pageAngle = this.nextPageAngle;
            PlayerEntity playerentity = tileEntityIn.getPlayer();
            is_render=false;
            if (playerentity != null) {
                AtomicBoolean is_ling_hun_gan_zhi = new AtomicBoolean(false);
                Collection<EffectInstance > a=playerentity.getActivePotionEffects();
                playerentity.getActivePotionEffects().forEach(instance -> {
                    if (instance.getPotion().equals(EffectRegistry.ling_hun_gan_zhi.get())) {
                        is_ling_hun_gan_zhi.set(true);
                    }
                });
                if (is_ling_hun_gan_zhi.get()) {
                    is_render=true;
                }
                double d0 = playerentity.getPosX() - (tileEntityIn.getPos().getX() + 0.5D);
                double d1 = playerentity.getPosZ() - (tileEntityIn.getPos().getZ() + 0.5D);
                this.field_195531_n = (float)MathHelper.atan2(d1, d0);
                this.nextPageTurningSpeed += 0.1F;
                if (this.nextPageTurningSpeed < 0.5F || random.nextInt(40) == 0) {
                    float f1 = this.field_195525_h;

                    do {
                        this.field_195525_h += (float)(random.nextInt(4) - random.nextInt(4));
                    } while(f1 == this.field_195525_h);
                }
            }
            else {
                this.field_195531_n += 0.02F;
                this.nextPageTurningSpeed -= 0.1F;
            }
                while (this.nextPageAngle >= (float) Math.PI) {
                    this.nextPageAngle -= ((float) Math.PI * 2F);
                }
                while (this.nextPageAngle < -(float) Math.PI) {
                    this.nextPageAngle += ((float) Math.PI * 2F);
                }
                while (this.field_195531_n >= (float) Math.PI) {
                    this.field_195531_n -= ((float) Math.PI * 2F);
                }
                while (this.field_195531_n < -(float) Math.PI) {
                    this.field_195531_n += ((float) Math.PI * 2F);
                }
                float f2;
                for (f2 = this.field_195531_n - this.nextPageAngle; f2 >= (float) Math.PI; f2 -= ((float) Math.PI * 2F)) {
                }

                while (f2 < -(float) Math.PI) {
                    f2 += ((float) Math.PI * 2F);
                }
                this.nextPageAngle += f2 * 0.4F;
                this.nextPageTurningSpeed = MathHelper.clamp(this.nextPageTurningSpeed, 0.0F, 1.0F);
                this.field_195524_g = this.field_195523_f;
                float f = (this.field_195525_h - this.field_195523_f) * 0.4F;
                f = MathHelper.clamp(f, -0.2F, 0.2F);
                this.field_195526_i += (f - this.field_195526_i) * 0.9F;
                this.field_195523_f += this.field_195526_i;
            if(is_render) {
                matrixStackIn.push();
                matrixStackIn.scale(0.5f,0.5f,0.5f);
                matrixStackIn.translate(1D, 1.75D, 1D);
                float fm = (float) ticks + partialTicks;
                matrixStackIn.translate(0.0D, (double) (0.1F + MathHelper.sin(fm * 0.1F) * 0.01F), 0.0D);

                float f1m;
                for (f1m = nextPageAngle - pageAngle; f1m >= (float) Math.PI; f1m -= ((float) Math.PI * 2F)) {
                }

                while (f1m < -(float) Math.PI) {
                    f1m += ((float) Math.PI * 2F);
                }

                float f2m = pageAngle + f1m * partialTicks;
                matrixStackIn.rotate(Vector3f.YP.rotation(-f2m + (float) (Math.PI / 2 * 3)));
                matrixStackIn.rotate(Vector3f.ZP.rotationDegrees(90.0F));
                matrixStackIn.translate(1, 0, 0);
                if(list.size()==0){
                    ItemStack danYaoItemStack = DanYao.getDanYao(tileEntityIn.getFinishItem(),tileEntityIn.getQuality());
                    if(danYaoItemStack!=null) {
                        matrixStackIn.rotate(Vector3f.ZP.rotationDegrees(-90.0F));
                        ItemRenderer itemRenderer = Minecraft.getInstance().getItemRenderer();
                        IBakedModel ibakedmodel = itemRenderer.getItemModelWithOverrides(danYaoItemStack, tileEntityIn.getWorld(), null);
                        itemRenderer.renderItem(danYaoItemStack, ItemCameraTransforms.TransformType.FIXED, true, matrixStackIn,
                                bufferIn, combinedLightIn, combinedOverlayIn, ibakedmodel);
                        matrixStackIn.rotate(Vector3f.ZP.rotationDegrees(90.0F));

                        matrixStackIn.translate(0, 1, 0);
                    }
                }
                matrixStackIn.translate(0, -(list.size() - 1) * 0.5, 0);
                ItemRenderer itemRenderer = Minecraft.getInstance().getItemRenderer();
                ItemStack stack;
                for (int i = 0; i < list.size(); ++i) {
                    matrixStackIn.rotate(Vector3f.ZP.rotationDegrees(-90.0F));
                    matrixStackIn.translate(0,0 , -0.001);
                    stack=list.get(i);
                    itemRenderer.renderItem(stack, ItemCameraTransforms.TransformType.FIXED, true, matrixStackIn,
                            bufferIn, combinedLightIn, combinedOverlayIn, itemRenderer.getItemModelWithOverrides(stack, tileEntityIn.getWorld(), null));
                    matrixStackIn.translate(0,0 , 0.001);
                    stack = cai_liao_bg[tileEntityIn.getCaiLiaoTempratureNum(i)];
                    itemRenderer.renderItem(stack, ItemCameraTransforms.TransformType.FIXED, true, matrixStackIn,
                            bufferIn, combinedLightIn, combinedOverlayIn, itemRenderer.getItemModelWithOverrides(stack, tileEntityIn.getWorld(), null));
                    stack=cai_liao_board_time[tileEntityIn.getCaiLiaoTimeNum(i)];
                    itemRenderer.renderItem(stack, ItemCameraTransforms.TransformType.FIXED, true, matrixStackIn,
                            bufferIn, combinedLightIn, combinedOverlayIn, itemRenderer.getItemModelWithOverrides(stack, tileEntityIn.getWorld(), null));
                    stack=cai_liao_board_point[tileEntityIn.getCaiLiaoPointNum(i)];
                    matrixStackIn.translate(0, 1.2, 0);
                    itemRenderer.renderItem(stack, ItemCameraTransforms.TransformType.FIXED, true, matrixStackIn,
                            bufferIn, combinedLightIn, combinedOverlayIn, itemRenderer.getItemModelWithOverrides(stack, tileEntityIn.getWorld(), null));
                    matrixStackIn.translate(0, -1.2, 0);
                    matrixStackIn.rotate(Vector3f.ZP.rotationDegrees(90.0F));
                    matrixStackIn.translate(0, 1.1, 0);
                }
                matrixStackIn.pop();
            }
        }

        //显示粒子
        if(tileEntityIn.isEvenFinishing()){
            tileEntityIn.getWorld().playSound(tileEntityIn.getPos().getX(), tileEntityIn.getPos().getY(), tileEntityIn.getPos().getZ(), SoundEvents.BLOCK_BEACON_ACTIVATE, SoundCategory.BLOCKS, 1.0F, 1.0F, false);
            for(int j =0;j<20;++j){
                double d0 = (double) tileEntityIn.getPos().getX() + Math.random() * 3 - 1.5;
                double d1 = (double) tileEntityIn.getPos().getY() + Math.random() * 3 - 1.5;
                double d2 = (double) tileEntityIn.getPos().getZ() + Math.random() * 3 - 1.5;
                tileEntityIn.getWorld().addParticle(ParticleTypes.HAPPY_VILLAGER, d0, d1+2, d2, Math.random() * 0.02D, Math.random() * 0.02D, Math.random() * 0.02D);
            }
        }
        if(tileEntityIn.isEvenBad()){
            tileEntityIn.getWorld().playSound(tileEntityIn.getPos().getX(), tileEntityIn.getPos().getY(), tileEntityIn.getPos().getZ(), SoundEvents.ENTITY_DONKEY_ANGRY, SoundCategory.BLOCKS, 1.0F, 1.0F, false);
            for(int j =0;j<20;++j){
                double d0 = (double) tileEntityIn.getPos().getX() + Math.random() * 3 - 1.5;
                double d1 = (double) tileEntityIn.getPos().getY() + Math.random() * 3 - 1.5;
                double d2 = (double) tileEntityIn.getPos().getZ() + Math.random() * 3 - 1.5;
                tileEntityIn.getWorld().addParticle(ParticleTypes.ANGRY_VILLAGER, d0, d1+2, d2, Math.random() * 0.02D, Math.random() * 0.02D, Math.random() * 0.02D);
            }
        }
        //显示状态
        matrixStackIn.push();
        ItemRenderer itemRenderer = Minecraft.getInstance().getItemRenderer();
        matrixStackIn.scale(0.75f,0.75f,0.75f);
        switch (tileEntityIn.getBlockState().get(FACING)){
            case NORTH:{
                matrixStackIn.translate(0.65, 0.6, 1.35);
                break;
            }
            case WEST:{
                matrixStackIn.rotate(Vector3f.YP.rotationDegrees(90.0F));
                matrixStackIn.translate(-0.65, 0.6, 1.35);
                break;
            }
            case SOUTH:{
                matrixStackIn.translate(0.65, 0.6, 0);
                break;
            }
            case EAST:{
                matrixStackIn.rotate(Vector3f.YP.rotationDegrees(90.0F));
                matrixStackIn.translate(-0.65, 0.6, 0);
                break;
            }
        }
        itemRenderer.renderItem(qian_kou_1[tileEntityIn.getQianKouNum()], ItemCameraTransforms.TransformType.FIXED, true, matrixStackIn, bufferIn, combinedLightIn, combinedOverlayIn,
                itemRenderer.getItemModelWithOverrides(qian_kou_1[tileEntityIn.getQianKouNum()], tileEntityIn.getWorld(), null));
        if(tileEntityIn.isShouldFire()){
            switch (tileEntityIn.getBlockState().get(FACING)){
                case NORTH:{
                    matrixStackIn.translate(0.02, 0, 0.02);
                    break;
                }
                case WEST:{
                    matrixStackIn.translate(0.02, 0, 0.02);
                    break;
                }
                case SOUTH:{
                    matrixStackIn.translate(0.02, 0, -0.02);
                    break;
                }
                case EAST:{
                    matrixStackIn.translate(-0.02, 0, -0.02);
                    break;
                }
            }
            itemRenderer.renderItem(qian_kou_fire[Math.abs(ticks)/8%8], ItemCameraTransforms.TransformType.FIXED, true, matrixStackIn, bufferIn, combinedLightIn, combinedOverlayIn,
                    itemRenderer.getItemModelWithOverrides(qian_kou_fire[ticks/8%8], tileEntityIn.getWorld(), null));
        }
        matrixStackIn.pop();
    }
    public static final DirectionProperty FACING = HorizontalBlock.HORIZONTAL_FACING;
}
